package com.bicycle.rentalsystem.servelt.user;

import com.bicycle.rentalsystem.pojo.user.MessageInfo;
import com.bicycle.rentalsystem.service.user.OnlineService;
import com.bicycle.rentalsystem.service.impl.user.OnlineServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(value = {"/user/online","/user/addMessageInfo"})
public class OnlineServlet extends HttpServlet {

    private OnlineService onlineService = new OnlineServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<MessageInfo> messageList = onlineService.searchMessages();
        HttpSession session = req.getSession();
        session.setAttribute("messageList",messageList);
        resp.sendRedirect("online.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String message = req.getParameter("message");
        String username =(String) session.getAttribute("username");
        int result = onlineService.addMessage(username, message);
        if (result == 1){
            resp.sendRedirect("online");
        }
    }
}
